package com.sbdemo.controller;

import com.sbdemo.bean.TestBean;
import com.sbdemo.service.ITestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by Administrator on 2017/3/7.
 */
@RestController
@RequestMapping(value = "/test")
public class TestController {

    @Autowired
    private ITestService testService;

    @RequestMapping(value = "/fOne")
    public TestBean findOne(Long id){
        return testService.findOne(id);
    }

    @RequestMapping(value = "/fAll")
    public List<TestBean> findAll(){
        return testService.findAll();
    }

    @RequestMapping(value = "/add")
    public boolean addTestBean(String name,String age){
//        return testService.addTestBean(t);
        TestBean t = new TestBean(name,age);
        return testService.addTestBean(t);
    }

/*    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public boolean addTestBean(String data){
//        return testService.addTestBean(t);
        return false;
    }*/

    @RequestMapping(value = "/del")
    public boolean delTestBean(String ids){
        return testService.delTestBean(parseStrIds(ids));
    }

    @RequestMapping(value = "/up")
    public void update(String name,Long id,String age){
        TestBean t= new TestBean();
        t.setId(id);
        t.setName(name);
        t.setAge(age);
        testService.updateTestBean(t);
    }

    public static Long[] parseStrIds(String delIds){
        Long[] ids=null;

        if(delIds!=null&&delIds.length()>0){

            String hids[]=delIds.split(",");

            ids=new Long[hids.length];

            for (int i = 0; i < hids.length; i++) {
                String idstr = hids[i].replace(" ", "");
                if (idstr != null && !"".equals(idstr)) {
                    ids[i] = Long.parseLong(idstr);
                }
            }
        }
        return ids;
    }
}
